﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class ElevatorJudgeInfoController : BaseController
    {
        public ElevatorInfoApp _ElevatorInfoApp;

        public ElevatorJudgeInfoController(ElevatorInfoApp elevatorInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            _ElevatorInfoApp = elevatorInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult ElevatorJudgeInfoAsync()
        {
            return View();
        }
        public  async Task<JsonResult> GetElevatorJudgeInfosAsync()
        {
            ElevatorJudgeInfoDto elevatorJudgeInfoDto = new();

            elevatorJudgeInfoDto.PageIndex = (Start / Length) + 1;
            elevatorJudgeInfoDto.PageSize = Length;
            elevatorJudgeInfoDto.JudgeEquipmentCode = Request.Form["judgeEquipmentCode"];
            var elevatorinfos = await _ElevatorInfoApp.GetElevatorJudgeInfosAsync(elevatorJudgeInfoDto);

            var pageData = new TableData<ElevatorJudgeInfoDto>(Draw, elevatorinfos.Total, elevatorinfos.Total, elevatorinfos.Result);
            //await AddTracesInfo(5, 0, "获取堆垛机作业模式数据");
            return Json(pageData);
        }

        public async Task<JsonResult> ModifyEleStatusAsync()
        {
            var id = Request.Form["id"].ToInt64();
            var isEnable = Request.Form["isEnable"].ToBool();
            bool result;
            string msg = "禁用";
            try
            {


                var elevatorInfoDto = await _ElevatorInfoApp.GetElevatorJudgeInfoByIDAsync(id);
                if (isEnable)
                {
                    msg = "启用";
                }
                if (elevatorInfoDto == null)
                {
                    result = false;
                    msg += "查询失败";
                    return CreateJsonGetResult(result, msg);
                }
                elevatorInfoDto.IsEnable = !isEnable;
                try
                {
                    result = await _ElevatorInfoApp.UpdateElevatorJudgeAsync(elevatorInfoDto);
                    msg += "成功!";
                }
                catch (Exception e)
                {
                    result = false;
                    msg = e.Message;
                }
            }
            catch (Exception e)
            {

                throw;
            }
            return CreateJsonGetResult(result, msg);
            //}
        }

        public async Task<JsonResult> ModifyEleUseAsync()
        {
            var id = Request.Form["id"].ToInt64();
            var isNextEnd = Request.Form["isNextEnd"].ToBool();
            bool result;
            string msg = "设为终结点";
            try
            {


                var elevatorInfoDto = await _ElevatorInfoApp.GetElevatorJudgeInfoByIDAsync(id);
                if (isNextEnd)
                {
                    msg = "设为终结点";
                }
                if (elevatorInfoDto == null)
                {
                    result = false;
                    msg += "查询失败";
                    return CreateJsonGetResult(result, msg);
                }
                elevatorInfoDto.IsNextEnd = !isNextEnd;
                try
                {
                    result = await _ElevatorInfoApp.UpdateElevatorJudgeAsync(elevatorInfoDto);
                    msg += "成功!";
                }
                catch (Exception e)
                {
                    result = false;
                    msg = e.Message;
                }
            }
            catch (Exception e)
            {

                throw;
            }
            return CreateJsonGetResult(result, msg);
            //}
        }
    }
}
